package com.sk.skplanet.commute.actions;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.sk.skplanet.commute.exceptions.BusAPIException;
import com.sk.skplanet.commute.vo.Member;
import com.sk.skplanet.commute.vo.Point;
import com.sk.skplanet.commute.vo.Route;
import com.sk.skplanet.commute.vo.StopInfo;

public class JsonParsing {
	public static Member parseMemberFromJsonObject(JSONObject jObject) throws JSONException, BusAPIException {
		String result = jObject.getString("success");
		
		if(result.equals("ERROR")) {
			throw new BusAPIException(jObject.getString("message"));
		}
		JSONObject data = jObject.getJSONObject("data");
		Member member = new Member(data.getInt("id"));
		// Member data = new Member(jObject.getInt("id"), jObject.getString("email"), jObject.getString("password"));

		List<Route> routeList = JsonParsing.parseRoutesFromJsonArray(data.getJSONArray("routes"));
		for (int i = 0; i < routeList.size(); i++) {
			member.addRoute(routeList.get(i));
		}
		Log.i("member",member.toString());
		
//		if(member.getRoutes()==null)member.setRoutes(new ArrayList<Route>());

		return member;
	}

	public static List<Route> parseRoutesFromJsonArray(JSONArray jArray) throws JSONException {
		List<Route> list = new ArrayList<Route>();

		for (int i = 0; i < jArray.length(); i++) {
			JSONObject route_json_data = jArray.getJSONObject(i);
			Route data = new Route(route_json_data.getInt("id"), route_json_data.getString("name"),
					route_json_data.getInt("memberId"));

			List<Point> pointList = JsonParsing.parsePointsFromJsonArray(route_json_data.getJSONArray("points"));
			for (int k = 0; k < pointList.size(); k++) {
				data.addPoint(pointList.get(k));
			}
			list.add(data);
		}
		return list;
	}

	public static List<Point> parsePointsFromJsonArray(JSONArray jArray) throws JSONException {
		List<Point> list = new ArrayList<Point>();

		for (int i = 0; i < jArray.length(); i++) {
			JSONObject point_json_data = jArray.getJSONObject(i);
			Point data = new Point(point_json_data.getInt("id"), point_json_data.getString("name"),
					point_json_data.getInt("routeId"));

			data.setStopInfos(JsonParsing.parseStopInfosFromJsonArray(point_json_data.getJSONArray("stopinfos")));
			list.add(data);
		}
		return list;
	}

	public static List<StopInfo> parseStopInfosFromJsonArray(JSONArray jArray) throws JSONException {
		List<StopInfo> list = new ArrayList<StopInfo>();

		for (int i = 0; i < jArray.length(); i++) {
			JSONObject stopinfo_json_data = jArray.getJSONObject(i);
			StopInfo data = new StopInfo(stopinfo_json_data.getInt("id"), stopinfo_json_data.getString("stopId"),
					stopinfo_json_data.getString("stopName"), stopinfo_json_data.getString("busId"),
					stopinfo_json_data.getString("busName"), stopinfo_json_data.getInt("pointId"));
			data.setBus_type(stopinfo_json_data.getString("busType"));
			list.add(data);
		}
		return list;
	}

}
